#include "StdAfx.h"
#include "WPContext.h"
#include <shlwapi.h>

CWPContext::CWPContext(void)
{
}

CWPContext::~CWPContext(void)
{

}

void CWPContext::Clear()
{
    m_mciWave.Stop();
    m_mciWave.CloseDevice();

    m_WaveRecorder.Close();
}

BOOL CWPContext::Init()
{
    memset(m_szFileName, 0, sizeof(m_szFileName));
    ::GetCurrentDirectory(sizeof(m_szFileName), m_szFileName);
    _tcscat(m_szFileName, _T("\\wpcontext.ini"));

    //if (!PathFileExists(m_szFileName))
    //{
    //    int fp = _topen(m_szFileName, _O_CREAT);
    //    _close(fp);
    //}
    
    memset(m_szWPMainSec, 0, sizeof(m_szWPMainSec));
    _tcscpy_s(m_szWPMainSec, _countof(m_szWPMainSec), _T("WPMain"));

    BOOL bRet = m_ttsWrapper.Init();

    if (ERROR_SUCCESS != m_WaveRecorder.Open())
    {
        //strErr = _T("系统没有检测到录音设备，本软件无法运行。");
        bRet |= FALSE;
    }
    TRACE(_T("wave in volume:%d\n"), m_WaveRecorder.GetWaveInVolume());

    if (ERROR_SUCCESS != m_mciWave.OpenDevice())
    {
        //strErr = _T("系统没有检测到音频设备，本软件无法运行。");
        bRet |= FALSE;
    }
    
    m_ssEvalMgr.Open();

    GetModuleFileName(NULL, m_szRecordFile, MAX_PATH);
    TCHAR* p = _tcsrchr(m_szRecordFile, '\\');
    if (p)
    {
        *p = '\0';
    }
    _stprintf(m_szRecordFile, _T("%s\\sound\\wordrecord.wav"), m_szRecordFile);

    return bRet;
}

void CWPContext::SetLevel(LPCTSTR levelName)
{
    WritePrivateProfileString(m_szWPMainSec, _T("Level"), levelName, m_szFileName);
}

LPCTSTR CWPContext::GetLevel()
{
    memset(m_szValue, 0, sizeof(m_szValue));
    GetPrivateProfileString(m_szWPMainSec, _T("Level"), _T("小学"),
        m_szValue, _countof(m_szValue), m_szFileName);

    return m_szValue;
}


void CWPContext::SetTeachingMaterial(LPCTSTR materialName)
{
    WritePrivateProfileString(m_szWPMainSec, _T("TeachingMaterial"), materialName, m_szFileName);
}

LPCTSTR CWPContext::GetTeachingMaterial()
{
    memset(m_szValue, 0, sizeof(m_szValue));
    GetPrivateProfileString(m_szWPMainSec, _T("TeachingMaterial"), _T("-1_请选择教材"),
        m_szValue, _countof(m_szValue), m_szFileName);

    return m_szValue;
}

void CWPContext::SetGrade(LPCTSTR gradeName)
{
    WritePrivateProfileString(m_szWPMainSec, _T("Grade"), gradeName, m_szFileName);
}

LPCTSTR CWPContext::GetGrade()
{
    memset(m_szValue, 0, sizeof(m_szValue));
    GetPrivateProfileString(m_szWPMainSec, _T("Grade"), _T(""),
        m_szValue, _countof(m_szValue), m_szFileName);

    return m_szValue;
}

void CWPContext::SetUnit(LPCTSTR unitName)
{
    WritePrivateProfileString(m_szWPMainSec, _T("Unit"), unitName, m_szFileName);
}

LPCTSTR CWPContext::GetUnit()
{
    memset(m_szValue, 0, sizeof(m_szValue));
    GetPrivateProfileString(m_szWPMainSec, _T("Unit"), _T(""),
        m_szValue, _countof(m_szValue), m_szFileName);

    return m_szValue;
}

void CWPContext::PlayMusicForStudyWord(int nGate, HWND hWnd /* = NULL */)
{
    TCHAR szPath[512] = {0};
    TCHAR szSoundFile[512] = {0};
    GetModuleFileName(NULL, szPath, MAX_PATH);
    TCHAR* p = _tcsrchr(szPath, '\\');
    if (p)
    {
        *p = '\0';
    }
    _stprintf(szSoundFile, _T("%s\\sound\\%d.mp3"), szPath, nGate);
    m_mciWave.Stop();
    m_mciWave.Play(hWnd, szSoundFile);
}

void CWPContext::PlayRecordFile(HWND hWnd /* = NULL */, BOOL bWait /* = FALSE */)
{
    m_mciWave.Stop();
    if (bWait)
    {
        m_mciWave.PlayWait(hWnd, m_szRecordFile);
    }
    else
    {
        m_mciWave.Play(hWnd, m_szRecordFile);
    }
}

void CWPContext::PlayMusiceForTrue(HWND hWnd /* = NULL */, BOOL bWait /* = FALSE */)
{
    TCHAR szPath[512] = {0};
    TCHAR szSoundFile[512] = {0};
    GetModuleFileName(NULL, szPath, MAX_PATH);
    TCHAR* p = _tcsrchr(szPath, '\\');
    if (p)
    {
        *p = '\0';
    }
    _stprintf(szSoundFile, _T("%s\\sound\\true.mp3"), szPath);
    m_mciWave.Stop();
    if (bWait)
    {
        m_mciWave.PlayWait(hWnd, szSoundFile);
    }
    else
    {
        m_mciWave.Play(hWnd, szSoundFile);
    }
} 

void CWPContext::PlayMusiceForFalse(HWND hWnd /* = NULL */, BOOL bWait /* = FALSE */)
{
    TCHAR szPath[512] = {0};
    TCHAR szSoundFile[512] = {0};
    GetModuleFileName(NULL, szPath, MAX_PATH);
    TCHAR* p = _tcsrchr(szPath, '\\');
    if (p)
    {
        *p = '\0';
    }
    _stprintf(szSoundFile, _T("%s\\sound\\false.mp3"), szPath);
    m_mciWave.Stop();
    if (bWait)
    {
        m_mciWave.PlayWait(hWnd, szSoundFile);
    }
    else
    {
        m_mciWave.Play(hWnd, szSoundFile);
    }
}

void CWPContext::PlayThreeSecForCountDown(HWND hWnd /* = NULL */, BOOL bWait /* = FALSE */)
{
    TCHAR szPath[512] = {0};
    TCHAR szSoundFile[512] = {0};
    GetModuleFileName(NULL, szPath, MAX_PATH);
    TCHAR* p = _tcsrchr(szPath, '\\');
    if (p)
    {
        *p = '\0';
    }
    _stprintf(szSoundFile, _T("%s\\sound\\three_sec.mp3"), szPath);
    m_mciWave.Stop();
    if (bWait)
    {
        m_mciWave.PlayWait(hWnd, szSoundFile);
    }
    else
    {
        m_mciWave.Play(hWnd, szSoundFile);
    }
}

void CWPContext::PlayMusiceForVictory(HWND hWnd /* = NULL */, BOOL bWait /* = FALSE */)
{
    TCHAR szPath[512] = {0};
    TCHAR szSoundFile[512] = {0};
    GetModuleFileName(NULL, szPath, MAX_PATH);
    TCHAR* p = _tcsrchr(szPath, '\\');
    if (p)
    {
        *p = '\0';
    }
    _stprintf(szSoundFile, _T("%s\\sound\\victory.mp3"), szPath);
    m_mciWave.Stop();
    if (bWait)
    {
        m_mciWave.PlayWait(hWnd, szSoundFile);
    }
    else
    {
        m_mciWave.Play(hWnd, szSoundFile);
    }
}

void CWPContext::PlayMusicForDing(HWND hWnd /* = NULL */, BOOL bWait /* = FALSE */)
{
    TCHAR szPath[512] = {0};
    TCHAR szSoundFile[512] = {0};
    GetModuleFileName(NULL, szPath, MAX_PATH);
    TCHAR* p = _tcsrchr(szPath, '\\');
    if (p)
    {
        *p = '\0';
    }
    _stprintf(szSoundFile, _T("%s\\sound\\ding.wav"), szPath);
    m_mciWave.Stop();
    if (bWait)
    {
        m_mciWave.PlayWait(hWnd, szSoundFile);
    }
    else
    {
        m_mciWave.Play(hWnd, szSoundFile);
    }
}

int CWPContext::GetSecondsByWord(LPCTSTR lpszWord)
{
    size_t nLen = _tcslen(lpszWord);
    int nSeconds = (nLen * 200) / 1000;
    if (nSeconds < 3)
    {
        nSeconds = 3;
    }

    return nSeconds;
}

int CWPContext::GetSecondsBySentence(LPCTSTR lpszSent)
{
    TCHAR szSent[1024] = {0};
    _tcscpy(szSent, lpszSent);
    TCHAR seps[]   = _T(" ,");
    TCHAR *token;
    int nCount = 0;
    token = _tcstok( szSent, seps ); // C4996
    // Note: strtok is deprecated; consider using strtok_s instead
    while( token != NULL )
    {
        // Get next token: 
        token = _tcstok( NULL, seps ); // C4996
        nCount++;
    }

    int nSeconds = 2 + nCount*0.6;
    return nSeconds;
}

DWORD CWPContext::SpeechRecognition_Start()
{
    m_WaveRecorder.StopRecord();
    HRESULT hr = m_WaveRecorder.StartRecord(m_szRecordFile);
    return hr;
}

void CWPContext::SpeechRecognition_Stop()
{
    m_WaveRecorder.StopRecord();
    //m_mciWave.Stop();
}

void CWPContext::SingSoundPronEval(LPCTSTR lpSpokenText, LPTSTR lpszErrMsg)
{
    TCHAR szId[64] = {0};
    int nRet = m_ssEvalMgr.EvalWord( m_szRecordFile, lpSpokenText, szId );
}